package com.hebeiwangxiaozhang.admin.controller;

import com.hebeiwangxiaozhang.admin.pojo.CommodityCategory;
import com.hebeiwangxiaozhang.admin.service.CommodityCategoryService;
import com.hebeiwangxiaozhang.response.CommonResponse;
import com.hebeiwangxiaozhang.response.ResponseCode;
import com.hebeiwangxiaozhang.response.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/commodity/category")
public class CommodityCategoryController {

    @Autowired
    private CommodityCategoryService commodityCategoryService;

    //0. 查询商品类目
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResponse getCategoryList() {
        try {
            return ResponseUtils.okResponse(commodityCategoryService.getCategoryList());
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null,
                    e.getMessage());
        }
    }


    //1.新增商品类目
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResponse createCategory(@RequestBody CommodityCategory commodityCategory) {
        try {
            commodityCategoryService.create(commodityCategory);
            return ResponseUtils.okCreatedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

    //2. 删除商品类目
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public CommonResponse deleteCategory(@PathVariable("id") Long id) {
        try {
            commodityCategoryService.delete(id);
            return ResponseUtils.okDeletedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }
    //3. 更新商品类目
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResponse updateCategory(@RequestBody CommodityCategory commodityCategory) {
        try {
            commodityCategoryService.update(commodityCategory);
            return ResponseUtils.okUpdatedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

    @RequestMapping(value = "/createBrandRelation", method = RequestMethod.POST)
    public CommonResponse createBrandRelation(@RequestParam(value = "brandId") Long brandId,
                                                @RequestParam(value = "categoryId") Long categoryId) {
        try {
            commodityCategoryService.createBrandRelation(brandId, categoryId);
            return ResponseUtils.okCreatedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

    @RequestMapping(value = "/deleteBrandRelation", method = RequestMethod.POST)
    public CommonResponse deleteBrandRelation(@RequestParam(value = "brandId") Long brandId,
                                              @RequestParam(value = "categoryId") Long categoryId) {
        try {
            commodityCategoryService.deleteBrandRelation(brandId, categoryId);
            return ResponseUtils.okDeletedResponse();
        } catch (Exception e) {
            return ResponseUtils.failResponse(ResponseCode.BAD_REQUEST.getCode(), null, e.getMessage());
        }
    }

}
